<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2025-02-10 11:35:53
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-03-19 13:36:37
 * @FilePath: \global-brain\global-brain-admin\src\views\System\Knowledge\components\Edit.vue
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
-->
<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
// import { TableData } from '@/api/role/types'
import { sysKnowledgeSeeApi } from '@/api/sys/knowledge'
import { required } from '@/utils/formRules'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  },
  classData: {
    type: Array as PropType<any[]>,
    default: () => []
  }
})

const currentRow = reactive(props.currentRow)

const rules = reactive({
  org_name: [required]
})

const formSchema = reactive<FormSchema[]>([
  {
    field: 'knowledge_id',
    label: '主键',
    component: 'Input',
    componentProps: {
      disabled: true
    }
  },
  {
    field: 'title',
    label: '标题',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'type',
    label: '类型',
    component: 'Select',
    componentProps: {
      options: dict.sys_knowledge_type,
      onChange: (val) => handleTypeChange(val)
    }
  },
  {
    field: 'knowledge_class_id',
    label: '知识库分类',
    component: 'TreeSelect',
    componentProps: {
      data: props.classData,
      'check-strictly': true,
      'default-expand-all': true,
      'node-key': 'knowledge_class_id',
      props: {
        children: 'children',
        label: 'class_name'
      }
    }
  },
  {
    field: 'content',
    label: '内容',
    colProps: { span: 24 },
    component: 'Input',
    componentProps: {}
  }
])
const { register, methods, elFormRef } = useForm({
  schema: formSchema
})

// 根据选择类型切换不同的组件
const handleTypeChange = (val) => {
  // 首先清空内容
  if ('content' in currentRow && 'type' in currentRow) {
    if (currentRow.type != val) {
      currentRow.content = ''
    }
  }

  const contentSchema: any = formSchema.find((item) => item.field === 'content')
  // 根据选择类型切换不同的组件
  if (val == '1') {
    // 视频
    contentSchema.component = 'UploadVideo'
    contentSchema.componentProps = {}
  } else if (val == '2') {
    contentSchema.component = 'UploadImageMultiple'
    contentSchema.componentProps = {}
  } else if (val == '3') {
    contentSchema.component = 'UploadFile'
    contentSchema.componentProps = {}
  } else if (val == '4') {
    contentSchema.component = 'LeeTiptapEditor'
    contentSchema.componentProps = {}
  } else if (val == null) {
    contentSchema.component = 'Input'
    contentSchema.componentProps = {}
  } else {
    alert('该类型不存在')
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})

const getKnowledgeData = async () => {
  let res: any = await sysKnowledgeSeeApi({ knowledge_id: props.currentRow.knowledge_id })
  Object.assign(currentRow, res.data)
}

// 初始化
const init = async () => {
  // 查看详情接口
  await getKnowledgeData()
  // 根据选择类型切换不同的组件
  handleTypeChange(props.currentRow.type)
}
init()
</script>

<template>
  <Form :rules="rules" @register="register" />
</template>
